//
//  CycleRACBaseViewController.m
//  RACBaseControl
//
//  Created by 苹果笔记本 on 2020/5/13.
//  Copyright © 2020 base. All rights reserved.
//

#import "CycleRACBaseViewController.h"
#import "CycleRACBaseHeader.h"
@interface CycleRACBaseViewController ()

@end

@implementation CycleRACBaseViewController
///设置导航栏
- (void)cycle_setNavgation{}
///设置子控件
- (void)cycle_addSubViews{}
///订阅viewmodel数据
- (void)cycle_bindViewModel{}
///初次获取数据
- (void)cycle_getNewData{}
+(instancetype)allocWithZone:(struct _NSZone *)zone {
    CycleRACBaseViewController *vc = [super allocWithZone:zone];
    //didLoad
    @weakify(vc);
    [[vc rac_signalForSelector:@selector(viewDidLoad)] subscribeNext:^(id  _Nullable x) {
        @strongify(vc);
        [vc cycle_setNavgation];
        [vc cycle_addSubViews];
        [vc cycle_bindViewModel];
    }];
    [[vc rac_signalForSelector:@selector(viewWillAppear:)] subscribeNext:^(id  _Nullable x) {
        @strongify(vc);
        [vc cycle_getNewData];
    }];
    
    
    return vc;
}

/**
 * 是否需要前往登录
 */
- (void)cycle_shouldToLogin {
    if (1) {//判断代码
        //需要登录，前往登录
        dispatch_async(dispatch_get_main_queue(), ^{
           //跳转代码写这里
        });
        return;
    }
    //反之不需要登录，调用cycle_shouldToLogin也不会阻挡前进的步伐
}












#pragma mark --- 系统回调函数
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //添加隐藏键盘手势
    [self myrac_hideKeyBoard];
}

#pragma mark --- 添加隐藏键盘手势
- (void)myrac_hideKeyBoard {
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(keyboardHide:)];
    //设置成NO表示当前控件响应后会传播到其他控件上，默认为YES。
    tapGes.cancelsTouchesInView = NO;
    //将触摸事件添加到当前view
    [self.view addGestureRecognizer:tapGes];
    //注册键盘弹出通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    //注册键盘隐藏通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
}
///隐藏键盘
- (void)keyboardHide:(UITapGestureRecognizer *)tap {
    [self.view endEditing:YES];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
///键盘将要显示(视图上移)
- (void)keyboardWillShow:(NSNotification *)showNote {
    //首先通过这两行代码获取第一相应
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIView *firstResponder = [window performSelector:@selector(firstResponder)];
    //如果不是UITextField
    if ([firstResponder isKindOfClass:[UITextField class]] || [firstResponder isKindOfClass:[UITextView class]]) {
            //获取键盘高度
            //使用UIKeyboardFrameBeginUserInfoKey,会出现切换输入法时获取的搜狗键盘不对.
            CGFloat keyBoardHeight = [showNote.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
            CGRect frame = firstResponder.frame;
        
            CGFloat offSet = frame.origin.y + frame.size.height - (self.view.frame.size.height - keyBoardHeight); //iphone键盘高度为216.iped键盘高度为352
        
            [UIView beginAnimations:@"ResizeForKeyboardKey" context:nil];
        
            [UIView setAnimationDuration:0.5f];
        
            //将试图的Y坐标向上移动offset个单位，以使线面腾出开的地方用于软键盘的显示
            if (offSet > 0) {
                self.view.frame = CGRectMake(0.0f, -offSet, self.view.frame.size.width, self.view.frame.size.height);
                [UIView commitAnimations];
           }
//        //将rect由rect所在视图转换到目标视图view中，返回在目标视图view中的rect
//        CGRect rect = [firstResponder convertRect:firstResponder.frame toView:self.view];
//        NSLog(@"键盘即将出现");
//        //使用UIKeyboardFrameBeginUserInfoKey,会出现切换输入法时获取的搜狗键盘不对.
//        NSValue *value         = [[showNote userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
//        CGRect keyboardRect    = [value CGRectValue];
//        CGFloat keyboardHeight = keyboardRect.size.height;
//        NSLog(@"键盘高度:%f",keyboardHeight);
//
//        CGFloat offset = rect.origin.y+rect.size.height+keyboardHeight - self.view.bounds.size.height;
//
//        if (offset > 0) {
//            self.view.transform = CGAffineTransformMakeTranslation(0, -offset);
//        }
    }
    
    
    
}
///键盘将要隐藏(视图还原)
- (void)keyboardWillHide:(NSNotification *)hideNote {
//    self.view.transform = CGAffineTransformIdentity;
    self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}

/*
 // 将像素point由point所在视图转换到目标视图view中，返回在目标视图view中的像素值
 - (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
 // 将像素point从view中转换到当前视图中，返回在当前视图中的像素值
 - (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;
 
 // 将rect由rect所在视图转换到目标视图view中，返回在目标视图view中的rect
 - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;
 // 将rect从view中转换到当前视图中，返回在当前视图中的rect
 - (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;
 
 例把UITableViewCell中的subview(btn)的frame转换到 controllerA中
 
 // controllerA 中有一个UITableView, UITableView里有多行UITableVieCell，cell上放有一个button
 // 在controllerA中实现:
 CGRect rc = [cell convertRect:cell.btn.frame toView:self.view];
 或
 CGRect rc = [self.view convertRect:cell.btn.frame fromView:cell];
 // 此rc为btn在controllerA中的rect
 
 或当已知btn时：
 CGRect rc = [btn.superview convertRect:btn.frame toView:self.view];
 或
 CGRect rc = [self.view convertRect:btn.frame fromView:btn.superview];
 */
@end
